package com.rayo.accesscontrollercopy.model.db.bean;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.Ignore;
import com.litesuits.orm.db.annotation.Table;
import com.rayo.accesscontrollercopy.config.NetworkConfig;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yanghaojie on 16/7/27.
 */
@Table("keyuser_table")
public class KeyUserData extends BaseModel {
    public static final String USER_ID = "userid";

    @Column(USER_ID)
    private String userid;

    private String groupid;

    private String groupName;

    private String fname;

    private String lname;

    private String login;
    private String password;

    @JSONField(format = NetworkConfig.JSON_DATE_TIME)
    private Date begin;

    @JSONField(format = NetworkConfig.JSON_DATE_TIME)
    private Date end;

    private String valday;

    private int calendar;

   @Ignore
    private String plan;

    @Ignore
    private String deleteplan;

    @Ignore
    private List<PlanKeyTempData> templan;

    @Ignore
    private int flag;

    private boolean use;

    public KeyUserData() {
        this.userid = "0";
        this.groupid = "0";
        this.groupName = "";
        this.fname = "";
        this.lname = "";
        this.begin = new Date();
        this.end = new Date();
        this.valday = "0";
        this.calendar = 0;
        this.plan = "";
        this.deleteplan = "";
        this.templan = new ArrayList<PlanKeyTempData>();
        this.flag = 0;
        this.editime = new Date();
        this.login="";
        this.password="";
        this.use=false;

    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public void setGroupid(String groupid) {
        this.groupid = groupid;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getGroupid() {
        return groupid;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public Date getBegin() {
        return begin;
    }

    public void setBegin(Date begin) {
        this.begin = begin;
    }

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public String getValday() {
        return valday;
    }

    public void setValday(String valday) {
        this.valday = valday;
    }

    public String getPlan() {
        return plan;
    }

    public void setPlan(String plan) {
        this.plan = plan;
    }

    public String getDeleteplan() {
        return deleteplan;
    }

    public void setDeleteplan(String deleteplan) {
        this.deleteplan = deleteplan;
    }

    public List<PlanKeyTempData> getTemplan() {
        return templan;
    }

    public void setTemplan(List<PlanKeyTempData> templan) {
        this.templan = templan;
    }


    public int getCalendar() {
        return calendar;
    }

    public void setCalendar(int calendar) {
        this.calendar = calendar;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isUse() {
        return use;
    }

    public void setUse(boolean use) {
        this.use = use;
    }


    public static KeyUserData formJsonStr(JSONObject obj) {
        KeyUserData keyUserData = new KeyUserData();
        try {
            keyUserData.setUserid("userid");
            keyUserData.setValday(obj.getString("valday"));
            keyUserData.setLname(obj.getString("lname"));
            keyUserData.setCalendar(obj.getInteger("calendar"));
            keyUserData.setFname(obj.getString("fname"));
            keyUserData.setGroupid("groupid");

            SimpleDateFormat dateformat = new SimpleDateFormat(NetworkConfig.JSON_DATE_TIME);
            keyUserData.setBegin(dateformat.parse(obj.getString("begin")));
            keyUserData.setEnd(dateformat.parse(obj.getString("end")));

//            List<PlanKeyAdd> listAdd = keyUserData.getAddplan();
//            JSONArray arrayAdd = obj.getJSONArray("addplan");
//            for (int i = 0; i < arrayAdd.size(); i++) {
//                PlanKeyAdd addData = new PlanKeyAdd(Integer.valueOf(keyUserData.getUserid()));
//                addData.setPlanid(((JSONObject) arrayAdd.get(i)).getInteger("id"));
//                addData.setType(((JSONObject) arrayAdd.get(i)).getInteger("type"));
//                addData.setTimezoneid(((JSONObject) arrayAdd.get(i)).getInteger("timezoneid"));
//                listAdd.add(addData);
//            }
//
//            List<PlanKeyDel> listDel = keyUserData.getDeleteplan();
//            JSONArray arrayDel = obj.getJSONArray("deleteplan");
//            for (int i = 0; i < arrayDel.size(); i++) {
//                PlanKeyDel delData = new PlanKeyDel(Integer.valueOf(keyUserData.getUserid()));
//                delData.setPlanid(((JSONObject) arrayDel.get(i)).getInteger("id"));
//                delData.setType(((JSONObject) arrayDel.get(i)).getInteger("type"));
//                delData.setTimezoneid(((JSONObject) arrayDel.get(i)).getInteger("timezoneid"));
//                listDel.add(delData);
//            }
//            List<PlanKeyTempData> listTemp = keyUserData.getTemplan();
//            JSONArray arrayTemp = obj.getJSONArray("templan");
//            for (int i = 0; i < arrayTemp.size(); i++) {
//                PlanKeyTempData tempData = new PlanKeyTempData(Integer.valueOf(keyUserData.getUserid()));
//                JSONArray locklistArray = ((JSONObject) arrayTemp.get(i)).getJSONArray("locklist");
//                tempData.setLocklist(locklistArray.toJSONString());
//                tempData.setBegin(((JSONObject) arrayTemp.get(i)).getString("begin"));
//                tempData.setEnd(((JSONObject) arrayTemp.get(i)).getString("end"));
//                tempData.setExpiry(((JSONObject) arrayTemp.get(i)).getString("expiry"));
//                listTemp.add(tempData);
//            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return keyUserData;
    }
}
